package edu.jd.movieplatform.security.user;

import com.fasterxml.jackson.databind.util.JSONPObject;
import edu.jd.movieplatform.common.Result;
import org.apache.ibatis.annotations.Delete;
import org.apache.tomcat.util.json.JSONParser;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController //说明这是一个受spring容器管理的控制器组件(Bean)
@RequestMapping("/security/user") //本控制器的路径前缀
public class UserAPI {


    //引入依赖对象
    @Resource //此注解表示从容器中引入依赖对象
    private UserService userService;


    /**
     * 查询用户清单
     * @param dto
     * @return
     */
    @GetMapping("")
    public Result queryUsers(UserDto dto){

        Map<String,Object> page =  userService.getUserPage(dto);
        return Result.success(page);
    }

    @GetMapping("/{cust_username}")
    public Result queryUserById(@PathVariable String cust_username){
        User user =  userService.getUserById(cust_username);
        return Result.success(user);
    }


    /**
     * 修改用户信息
     * @param dto
     * @return
     */
    @PostMapping("")
    public Result addUser(@RequestBody UserDto dto){
        userService.addUser(dto);
        return Result.success();
    }

    /**
     * 修改用户信息
     * @param dto
     * @return
     */
    @PutMapping("")
    public Result updateUser(@RequestBody UserDto dto){
        userService.updateUser(dto);
        return Result.success();
    }

    /**
     * 按照账号删除一个用户信息
     * @param cust_username
     * @return
     */
    @DeleteMapping("/{cust_username}")
    public Result deleteUser(@PathVariable String cust_username,String user_status){

        userService.deleteOneUser(cust_username,user_status);
        return Result.success();
    }

    /**
     * 根据多个账号删除多个用户信息
     * @param
     * @return
     */
    @DeleteMapping("/del")
    public Result deleteUser(@RequestBody DelUserDto[] delUser){
        System.out.println(delUser);
        userService.deleteUser(delUser);
        return Result.success();
    }

}
